package com.haizhi.crm.recommend.service.imp;

import com.haizhi.crm.common.constant.CrmSchema;
import com.haizhi.crm.common.json.JSONUtils;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.util.Getter;
import com.haizhi.crm.recommend.constant.RecType;
import com.haizhi.crm.recommend.model.RecQo;
import com.haizhi.crm.recommend.model.RecVo;
import com.haizhi.crm.recommend.service.RecService;
import com.haizhi.crm.search.builder.SearchQoBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yangyijun on 2018/7/11.
 */
@Service
public class CompanyInterest extends BaseRecService implements RecService {
    public static final GLog LOG = new GLog(CompanyInterest.class);

    @Override
    public String getName() {
        return RecType.Company.DETAIL_INTERESTED;
    }

    @Override
    public RecVo doRecommend(RecQo recQo) {
        try {
            //查询公司的行业
            String industry = getIndustry(recQo);

            //查询行外事件
            List<Map<String, Object>> eventsMap = getOuterEvents(recQo, industry);
            return (RecVo) new RecVo().setPayload(eventsMap);
        } catch (Exception e) {
            LOG.error("recommend company industry interested error!", e);
            return (RecVo) new RecVo().setPayload(new ArrayList<>());
        }
    }

    private List<Map<String, Object>> getOuterEvents(RecQo recQo, String industry) {
        SearchQoBuilder eventBuilder = new SearchQoBuilder();
        eventBuilder.setGraph(recQo.getGraph());
        eventBuilder.addSchemas(CrmSchema.EVENT_OUTTER);
        eventBuilder.setPageNo(1);
        eventBuilder.setPageSize(5);
        eventBuilder.addTermFilter("industry", industry);
        eventBuilder.addTermFilter("type_", "新闻舆情" );
        eventBuilder.addSort("event_time", "desc" );
        Object events = searchService.search(eventBuilder.get()).getSearchResp().getData();
        List<Map<String, Object>> eventList = JSONUtils.toListMap(events);
        if (CollectionUtils.isEmpty(eventList)) {
            return new ArrayList<>();
        }

        List<String> detailKeys = new ArrayList<>();
        eventList.forEach(m -> detailKeys.add(Getter.get("detail_key", m)));

        SearchQoBuilder builder = new SearchQoBuilder();
        builder.setGraph(recQo.getGraph());
        builder.addSchemas(CrmSchema.EVENT_OUTTER_NEWS);
        builder.setPageNo(1);
        builder.setPageSize(5);
        builder.addTermFilter("object_key", detailKeys.toArray());
        builder.addSort("publish_time", "desc" );
        Object detail = searchService.search(builder.get()).getSearchResp().getData();
        return JSONUtils.toListMap(detail);
    }

    private String getIndustry(RecQo recQo) {
        String objectKey = recQo.getVertexId();
        SearchQoBuilder searchQoBuilder = new SearchQoBuilder();
        searchQoBuilder.setGraph(recQo.getGraph());
        searchQoBuilder.addSchemas(CrmSchema.COMPANY);
        searchQoBuilder.addTermFilter("object_key", objectKey);
        Object data = searchService.search(searchQoBuilder.get()).getSearchResp().getData();
        Map<String, Object> company = JSONUtils.toMap(data);
        return Getter.get("industry", company);
    }
}
